<?php
class Catalog_ProductController extends Areal_Controller_Action
{

    public function showAction()
    {
        $product_id = $this->getRequest()->getParam('product_id');
        $product = Doctrine::getTable('Catalog_Model_Product')->find($product_id);
        $this->view->product = $product;
    }

    public function searchAction()
    {
        $filter = new Catalog_Model_ProductSearchFilter();
        if ($this->getRequest()->isPost()){
            $filter->setData($this->getRequest()->getPost());
        }
        $page_number = $this->_getParam('page');
        $product_collection =  $filter->getFoundProductPaginator($page_number);
        $this->view->assign('products', $product_collection);
    }

    public function addAction()
    {
        $category_id = $this->getRequest()->getParam('category_id');
        $product = new Catalog_Model_Product();
        $product->category_id = $category_id;
        if ($this->getRequest()->isPost()){
            if ($product->edit($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('Product saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin','category','catalog',
                                                  array('category_id'=>$product->category_id));
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Product not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        } 
        $this->view->form = $product->getForm();
        Catalog_Model_Category::addCategoriesToNavigation($this->view->navigation(), $category_id);
        $this->view->category_id = $category_id;
    }

    public function editAction()
    {
        $product_id = $this->getRequest()->getParam('product_id');
        $product = Doctrine::getTable('Catalog_Model_Product')->find($product_id);
        if ($this->getRequest()->isPost()){
            if ($product->edit($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('Product saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin','category','catalog',
                                                  array('category_id'=>$product['category_id']) );
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Product not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        }
        $this->view->form = $product->getForm();
        Catalog_Model_Category::addCategoriesToNavigation($this->view->navigation(), $product->category_id);
    }
    
    public function deleteAction()
    {
        $product_id = $this->getRequest()->getParam('product_id');
        $product = Doctrine::getTable('Catalog_Model_Product')->find($product_id);
        $form = new Default_Form_ConfirmForm();
        if ($this->getRequest()->isPost()){
            if ($form->isValid($this->getRequest()->getPost())){
                $product->delete();
                $message = Zend_Registry::get('Zend_Translate')->translate('Product deleted');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin', 'category', 'catalog',
                       array('category_id'=>$product->category_id));
            }
        }
        $this->view->form = $form;
        Catalog_Model_Category::addCategoriesToNavigation($this->view->navigation(), $product->category_id);
    }


    public function thumbAction()
    {
        $product_id = $this->getRequest()->getParam('product_id');
        echo Catalog_Model_Product::getThumb($product_id);
        exit;
    }

    public function imageAction()
    {
        $product_id = $this->getRequest()->getParam('product_id');
        header('Content-Type: image/jpeg');
        echo Catalog_Model_Product::getImage($product_id);
        exit;
    }

    public function moveAction()
    {
        $product_id = $this->getRequest()->getParam('product_id');
        $product = Doctrine::getTable('Catalog_Model_Product')->find($product_id);
        if ($this->getRequest()->isPost()){
            if ($product->move($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('Product moved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin','category','catalog',
                                                  array('category_id'=>$product['category_id']) );
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Product not moved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        }
        $this->view->form = $product->getMoveForm();
        Catalog_Model_Category::addCategoriesToNavigation($this->view->navigation(), $product->category_id);
    }

}
